package service

import (
	"fmt"
	model2 "litter-api/app/model"
	"litter-api/app/util/pool"
)

type UserService struct {
	//UserAdd	*model2.UserAddr
}

func (h *UserService) Index() interface{} {
	var data *model2.WxUser
	pool.Db.Find(&data)

	return data
}

//Info
/**
 *author:Leedong
 *notes:用户信息
 *date:9:56 下午 2022/7/30
 **/
func (h *UserService) Info() (model2.WxUser, error) {
	var data model2.WxUser
	model := pool.Db.Model(data)
	model.Preload("Wallet")
	model.Preload("UserAddr")
	err := model.First(&data, model2.User.ID).Error
	if err != nil {
		return data, err
	}
	return data, nil
}

//Wallet
/**
 *author:Leedong
 *notes:用户信息&钱包
 *date:9:56 下午 2022/7/30
 **/
func (h *UserService) Wallet() (model2.WxWallet, error) {
	var data model2.WxWallet
	model := pool.Db.Model(data)
	model.Where("user_id = ?", model2.User.ID)
	err := model.First(&data).Error
	if err != nil {
		return data, err
	}
	return data, nil
}

func (h *UserService) UserAddr() ([]model2.UserAddr, error) {
	var data []model2.UserAddr
	model := pool.Db.Model(data)
	fmt.Println(model2.User)
	model.Where("user_id = ?", model2.User.ID)
	err := model.First(&data).Error
	if err != nil {
		return data, err
	}
	return data, nil
}

func (h *UserService) SetAddr(userAddr model2.UserAddr) error {
	userAddr.UserId = model2.User.ID
	model := pool.Db.Create(&userAddr)
	err := model.Error
	if err != nil {
		fmt.Println(err)
		return err
	}

	return nil
}

func (h *UserService) Login(openId string) int64 {
	var count int64
	pool.Db.Model(model2.WxUser{}).Where("openid = ?", openId).Count(&count)
	//countSting := strconv.FormatInt(count, 10)
	//count, _ = strconv.Atoi(countSting)
	return count
}
